<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="员工编号" prop="employeeNo">
        <el-input
          v-model="queryParams.employeeNo"
          placeholder="请输入员工编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="员工姓名" prop="uname">
        <el-input
          v-model="queryParams.uname"
          placeholder="请输入员工姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="使能有效期" prop="enable">
        <el-input
          v-model="queryParams.enable"
          placeholder="请输入使能有效期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="beginTime">
        <el-date-picker clearable
          v-model="queryParams.beginTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="用户邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入用户邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房间编号" prop="roomId">
        <el-input
          v-model="queryParams.roomId"
          placeholder="请输入房间编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卡号" prop="cardNo">
        <el-input
          v-model="queryParams.cardNo"
          placeholder="请输入卡号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门密码" prop="pwdNo">
        <el-input
          v-model="queryParams.pwdNo"
          placeholder="请输入门密码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开锁方式" prop="ulock">
        <el-input
          v-model="queryParams.ulock"
          placeholder="请输入开锁方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卡片id" prop="cardId">
        <el-input
          v-model="queryParams.cardId"
          placeholder="请输入卡片id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门禁" prop="controlIds">
        <el-input
          v-model="queryParams.controlIds"
          placeholder="请输入门禁"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型 " prop="types">
        <el-input
          v-model="queryParams.types"
          placeholder="请输入类型 "
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司名字" prop="company">
        <el-input
          v-model="queryParams.company"
          placeholder="请输入公司名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="来源" prop="froms">
        <el-input
          v-model="queryParams.froms"
          placeholder="请输入来源"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="拜访人姓名" prop="visitName">
        <el-input
          v-model="queryParams.visitName"
          placeholder="请输入拜访人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="拜访部门" prop="visitBumen">
        <el-input
          v-model="queryParams.visitBumen"
          placeholder="请输入拜访部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="拜访是由" prop="visitInfo">
        <el-input
          v-model="queryParams.visitInfo"
          placeholder="请输入拜访是由"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="photo">
        <el-input
          v-model="queryParams.photo"
          placeholder="请输入图片"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌" prop="plateNum">
        <el-input
          v-model="queryParams.plateNum"
          placeholder="请输入车牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['doorcontrol:user:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['doorcontrol:user:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['doorcontrol:user:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['doorcontrol:user:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="门禁人员" align="center" prop="id" />
      <el-table-column label="员工编号" align="center" prop="employeeNo" />
      <el-table-column label="员工姓名" align="center" prop="uname" />
      <el-table-column label="身份证" align="center" prop="idCard" />
      <el-table-column label="用户类型" align="center" prop="userType" />
      <el-table-column label="使能有效期" align="center" prop="enable" />
      <el-table-column label="开始时间" align="center" prop="beginTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.beginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户邮箱" align="center" prop="email" />
      <el-table-column label="手机号码" align="center" prop="phonenumber" />
      <el-table-column label="用户性别" align="center" prop="sex" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="房间编号" align="center" prop="roomId" />
      <el-table-column label="卡号" align="center" prop="cardNo" />
      <el-table-column label="门密码" align="center" prop="pwdNo" />
      <el-table-column label="状态 0正常 1删除" align="center" prop="status" />
      <el-table-column label="开锁方式" align="center" prop="ulock" />
      <el-table-column label="卡片id" align="center" prop="cardId" />
      <el-table-column label="门禁" align="center" prop="controlIds" />
      <el-table-column label="类型 " align="center" prop="types" />
      <el-table-column label="公司名字" align="center" prop="company" />
      <el-table-column label="来源" align="center" prop="froms" />
      <el-table-column label="拜访人姓名" align="center" prop="visitName" />
      <el-table-column label="拜访部门" align="center" prop="visitBumen" />
      <el-table-column label="拜访是由" align="center" prop="visitInfo" />
      <el-table-column label="录入图片" align="center" prop="photo" >
        <template  v-slot:default="scope">

          <el-image style="width: 70px; height: 50px;border-radius: 6px"
                    :src="scope.row.photo"
                    :preview-src-list="[scope.row.photo]"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="车牌" align="center" prop="plateNum" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['doorcontrol:user:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['doorcontrol:user:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改门禁用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="员工编号" prop="employeeNo">
          <el-input v-model="form.employeeNo" placeholder="请输入员工编号" />
        </el-form-item>
        <el-form-item label="员工姓名" prop="uname">
          <el-input v-model="form.uname" placeholder="请输入员工姓名" />
        </el-form-item>
        <el-form-item label="身份证" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证" />
        </el-form-item>
        <el-form-item label="使能有效期" prop="enable">
          <el-input v-model="form.enable" placeholder="请输入使能有效期" />
        </el-form-item>
        <el-form-item label="开始时间" prop="beginTime">
          <el-date-picker clearable
            v-model="form.beginTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="用户邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入用户邮箱" />
        </el-form-item>
        <el-form-item label="手机号码" prop="phonenumber">
          <el-input v-model="form.phonenumber" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="房间编号" prop="roomId">
          <el-input v-model="form.roomId" placeholder="请输入房间编号" />
        </el-form-item>
        <el-form-item label="卡号" prop="cardNo">
          <el-input v-model="form.cardNo" placeholder="请输入卡号" />
        </el-form-item>
        <el-form-item label="门密码" prop="pwdNo">
          <el-input v-model="form.pwdNo" placeholder="请输入门密码" />
        </el-form-item>
        <el-form-item label="开锁方式" prop="ulock">
          <el-input v-model="form.ulock" placeholder="请输入开锁方式" />
        </el-form-item>
        <el-form-item label="卡片id" prop="cardId">
          <el-input v-model="form.cardId" placeholder="请输入卡片id" />
        </el-form-item>
        <el-form-item label="门禁" prop="controlIds">
          <el-input v-model="form.controlIds" placeholder="请输入门禁" />
        </el-form-item>
        <el-form-item label="类型 " prop="types">
          <el-input v-model="form.types" placeholder="请输入类型 " />
        </el-form-item>
        <el-form-item label="公司名字" prop="company">
          <el-input v-model="form.company" placeholder="请输入公司名字" />
        </el-form-item>
        <el-form-item label="来源" prop="froms">
          <el-input v-model="form.froms" placeholder="请输入来源" />
        </el-form-item>
        <el-form-item label="拜访人姓名" prop="visitName">
          <el-input v-model="form.visitName" placeholder="请输入拜访人姓名" />
        </el-form-item>
        <el-form-item label="拜访部门" prop="visitBumen">
          <el-input v-model="form.visitBumen" placeholder="请输入拜访部门" />
        </el-form-item>
        <el-form-item label="拜访是由" prop="visitInfo">
          <el-input v-model="form.visitInfo" placeholder="请输入拜访是由" />
        </el-form-item>
        <el-form-item label="${comment}" prop="photo">
          <el-input v-model="form.photo" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="车牌" prop="plateNum">
          <el-input v-model="form.plateNum" placeholder="请输入车牌" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser } from "@/api/doorcontrol/user";

export default {
  name: "User",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 门禁用户表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        employeeNo: null,
        uname: null,
        idCard: null,
        userType: null,
        enable: null,
        beginTime: null,
        endTime: null,
        email: null,
        phonenumber: null,
        sex: null,
        roomId: null,
        cardNo: null,
        pwdNo: null,
        status: null,
        ulock: null,
        cardId: null,
        controlIds: null,
        types: null,
        company: null,
        froms: null,
        visitName: null,
        visitBumen: null,
        visitInfo: null,
        photo: null,
        plateNum: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        photo: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询门禁用户列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        employeeNo: null,
        uname: null,
        idCard: null,
        userType: null,
        enable: null,
        beginTime: null,
        endTime: null,
        createTime: null,
        createBy: null,
        email: null,
        phonenumber: null,
        sex: null,
        remark: null,
        roomId: null,
        cardNo: null,
        pwdNo: null,
        status: null,
        ulock: null,
        cardId: null,
        controlIds: null,
        types: null,
        company: null,
        froms: null,
        visitName: null,
        visitBumen: null,
        visitInfo: null,
        photo: null,
        plateNum: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加门禁用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改门禁用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除门禁用户编号为"' + ids + '"的数据项？').then(function() {
        return delUser(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('doorcontrol/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
